import {  message } from 'antd';

import 'antd/dist/antd.css';
const defaultState = {
    list: [{
        id:0,
        title:'这是演示1todolist',
        completed: false 
    },
    {
        id:1,
        title:'这是演示2todolist',
        completed: false 
    }

    ],
    list1:[]
  }
  const  reducer=(state = defaultState, payload) => {   
    switch (payload.type) {
        case 'PUT_DATA':
            if(payload.title ===''){
                alert("请输入内容")
                return state
            }
            message.success({
                content:'添加成功',
                duration:1,
            })
          return { 
              list: [
            ...state.list,
            {
              id: new Date().getTime(),
              title: payload.title,
              completed: false,
            }
          ]
        }
        case 'REMOVE_DATA':
            let newList1 = state.list.filter((item) => {
                return item.id !== payload.index
              })
              message.success({
                  content:'删除成功',
                  duration:1,
              })
            return {
                ...state,
                list: newList1
              } 

         case 'Change_DATA' :
            let newList2 = state.list.map(item => {
                const copy = { ...item }
                if (copy.id === payload.id) {
                  copy.completed = !copy.completed
                }
                return copy
              })
              return {
                ...state,
                list: newList2
              } 
          
        default:
          return state
      }
    }
    export default reducer